/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:assign-tracer
 * Create Time: 2016年1月21日 下午3:13:30
 */
package com.lenovo.lmrp.server.assigntracer.util.pusher;

import java.util.Date;
import java.util.Objects;

import com.lenovo.lmrp.base.mq.MQContent;
import com.lenovo.lmrp.base.mq.MQTopic;
import com.lenovo.sdc.framework.mq.AMQClient;
import com.lenovo.sdc.framework.util.JSONHelper;
import com.lenovo.sdc.framework.util.exception.ErrorCode;
import com.lenovo.sdc.framework.util.exception.TraceableException;

/**
 * @author songyz desc：消息发送接口
 */
public interface ISender {

	public void SendMessage(PushParameter PushParameter);

	public Runnable addMessageToList(PushParameter msParameter);

	public void retryPushMessage(PushParameter pushParameter);
	
	public final int MAX_SEND_ERROR_COUNT = 3;// 最大的重试次数

	public enum SendResult {
		SEND_SUCCEED(true,"发送成功"), SEND_FAILD(false,"发送失败");
		SendResult(boolean result,String desc) {
			this.result = result;
			this.desc = desc;
		}

		private boolean result;
		private String desc;

		public boolean getResult() {
			return result;
		}
	}
	public default void sendMQMessage(AMQClient mqClient,PushParameter pushParameter, boolean result){
		if (Objects.nonNull(mqClient)) {
			MQContent mqContent = new MQContent();
			SendMessageRecord record = new SendMessageRecord(pushParameter.getEngineerId(), new Date(),
					pushParameter.getSendType(), pushParameter.getContent(), result);
			mqContent.setContent(record);
			mqContent.setItsmcode(pushParameter.getItsmcode());
			mqContent.setSource(MQTopic.SENDMESSAGE.getSource());
			try {
				mqClient.publish(MQTopic.SENDMESSAGE.name(), JSONHelper.toJSON(mqContent));
			} catch (Exception e) {
				throw new TraceableException(ErrorCode.MQ_PUBLISH_FAILED, e);
			}
		}
	}
}
